package com.intellagric.controller.sso.module;

import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.SysUser;
import com.intellagric.service.sso.TokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther: zhy
 * @Date: 2018/10/7
 * @Description: 根据token查询用户信息
 */
@Controller
public class TokenController {

    @Autowired
    private TokenService tokenService;

    @RequestMapping("/user/token/{token}")
    @ResponseBody
    public Object getUser(@PathVariable String token ,String callback) {
        //ajax不能进行直接跨域请求
        /*return tokenService.getUserByToken(token);*/
        ResponseMessage userToken = tokenService.getUserByToken(token);
        if (StringUtils.isNotBlank(callback)) { //判断是否为jsonp请求
            //如果是，把结果封装成一个js语句响应（spring4.X以上版本写法）
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(userToken);
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        return userToken;
    }

    @RequestMapping("/user/token/edit")
    @ResponseBody
    public Object editUser(String token , SysUser sysUser,String callback) {
        ResponseMessage result = tokenService.editUserByToken(token, sysUser);
        if (StringUtils.isNotBlank(callback)) { //判断是否为jsonp请求
            //如果是，把结果封装成一个js语句响应（spring4.X以上版本写法）
            MappingJacksonValue mappingJacksonValue = new MappingJacksonValue(result);
            mappingJacksonValue.setJsonpFunction(callback);
            return mappingJacksonValue;
        }
        return result;
    }
}
